/**
 *
 */
package com.et114.modules.org.utility;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Date;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.et114.components.acegi.Constants;
import com.et114.components.acegi.cache.AcegiCacheManager;
import com.et114.core.exception.CheckException;
import com.et114.modules.org.services.OrgFactory;
import com.et114.modules.org.vo.GroupVo;
import com.et114.modules.org.vo.TitleMsgVo;
import com.et114.modules.org.vo.UserEmplyVo;
import com.et114.modules.org.vo.UserVo;

/*
 * @author guanhw
 */
public class SessionUserInfo {
	private static Log					logger			= LogFactory
																.getLog ( SessionUserInfo.class );
	private Long						userId;
	private String						loginName;
	private GroupVo						group;														// TODO
	private List						userProperties	= new ArrayList ( );						// TODO
	private GrantedAuthority [ ]		gas				= null;
	private Map							funcs			= new HashMap ( );
	private String						currentLoc;
	private Locale						locale			= Locale.getDefault ( );
	private String                      country         = null ; //= locale.getCountry ( ) ; 
	private String						language		= null ;//= locale.getLanguage ( ) ; 
	private String 						sessionID;
	private Date                        lastModPassTime ;
	private boolean                     isCheckPass = false ;
	private boolean                     checkPassState = false ; 
	private String                      checkedDefaultTargetUrl ;
	
	
	private byte currentUserMaxRoleType ; 
	public SessionUserInfo ( ) {
		// TODO Auto-generated constructor stub
		logger.info ( "" );
		try {
			initBean ( );
		} catch ( CheckException e ) {
			logger.error ( e.getMessage ( ) , e );
		}
	}
	
	private void initBean ( ) throws CheckException {
	}
	
	public GrantedAuthority [ ] getAuthorities ( ) throws Exception {
		if ( gas == null ) {
			UserDetails rd = AcegiCacheManager.getInstansce ( ).getUserDetailsByUserId ( loginName );
			gas = rd.getAuthorities ( );
			return gas;
		} else {
			return gas;
		}
	}
	
	public boolean hasAuthentication ( String str , byte type )
			throws Exception {
		if ( Constants.VOTER_ROLE_PREFIX_VALUE == type ) {
			str = Constants.VOTER_ROLE_PREFIX + str;
		} else if ( Constants.VOTER_TITLE_PREFIX_VALUE == type ) {
			str = Constants.VOTER_TITLE_PREFIX + str;
		} else if ( Constants.VOTER_USER_PREFIX_VALUE == type ) {
			str = Constants.VOTER_USER_PREFIX + str;
		} else {
			return false;
		}
		if ( gas == null ) {
			gas = getAuthorities ( );
		}
		for ( int i = 0 ; i < gas.length ; i ++ ) {
			GrantedAuthority ga = gas [ i ];
			String key = ga.getAuthority ( );
			if ( ga.toString ( ).equals ( key ) ) {
				return true;
			}
		}
		return false;
	}
	
	public UserVo getUserInfo ( ) throws CheckException {
		return OrgFactory.getUserManager ( ).getUserById ( userId );
	}
	
	public List getRoles ( ) throws CheckException {
		UserVo user = getUserInfo ( );
		OrgFactory.getUserManager ( ).userRels ( user );
		return user.getRoleList ( );
	}
	
	/**
	 * 取系统管理员 角色
	 */
	public List getRoles ( byte roleType ) throws CheckException {
		return  OrgFactory.getUserManager ( ).getAllRolesByUserParams (  roleType , userId );
	}
	
	//TODO
	public byte getUserRoleType ( ) throws CheckException {
		Integer type = OrgFactory.getUserManager ( ).getMaxRoleTypeByUserId ( userId ) ;
		if ( type == null || type.intValue ( ) == 0 ) {
			return Constants.ROLE_TYPE_USER - 1;
		}
		return type.byteValue ( ) ; 
	}
	
	// TODO
	public TitleMsgVo getTitle ( ) throws CheckException {
		return OrgFactory.getGroupManager ( ).loadPrimaryTitleOfUser ( userId );
	}
	
	public UserEmplyVo getUserEmply ( ) throws CheckException {
		return OrgFactory.getGroupManager ( ).loadEmpWithTitlesByUser ( userId );
	}
	
	public Long getUserId ( ) {
		return userId;
	}
	
	public void setUserId ( Long userId ) {
		this.userId = userId;
	}
	
	public void setLoginName ( String loginName ) {
		this.loginName = loginName;
	}
	
	public void setUserProperties ( List userProperties ) {
		this.userProperties = userProperties;
	}
	
	public List getUserProperties ( ) {
		return userProperties;
	}
	
	public GroupVo getGroup ( ) {
		UserEmplyVo userEmply = getUserEmply ( ) ; 
		if ( userEmply != null  ) {
			Long grpId = userEmply.getGrpId ( ) ; 
			if ( grpId != null && grpId.longValue ( ) != 0  ) {
				return OrgFactory.getGroupManager ( ).loadGroup ( grpId ) ; 
			}			
		}
		return group;
	}
	
	public String getLoginName ( ) {
		return loginName;
	}
	
	public Map getFuncs ( ) {
		return funcs;
	}
	
	public String getCurrentLoc ( ) {
		return currentLoc;
	}
	
	public void setCurrentLoc ( String currentLoc ) {
		this.currentLoc = currentLoc;
	}
	
	public String getLanguage ( ) {
		return ( language == null ? locale.getLanguage ( ) : language );
	}
	
	public void setLanguage ( String language ) {
		this.language = language;
	}
	
	public Locale getLocale ( ) {
		return locale;
	}
	  
	public void setLocale ( Locale locale ) {
		this.locale = locale;
	}

	public byte getCurrentUserMaxRoleType ( ) {
		currentUserMaxRoleType = getUserRoleType(  )  ;
		return currentUserMaxRoleType  ; 
	}

	public void setCurrentUserMaxRoleType ( byte currentUserMaxRoleType ) {
		this.currentUserMaxRoleType = currentUserMaxRoleType;
	}

	public String getSessionID ( ) {
		return sessionID;
	}

	public void setSessionID ( String sessionID ) {
		this.sessionID = sessionID;
	}

	public String getCountry ( ) {
		return ( country == null ? locale.getCountry ( ) : country );
	}

	public void setCountry ( String country ) {
		this.country = country;
	}

	public Date getLastModPassTime ( ) {
		return lastModPassTime;
	}

	public void setLastModPassTime ( Date lastModPassTime ) {
		this.lastModPassTime = lastModPassTime;
	}

	public boolean isCheckPass ( ) {
		return isCheckPass;
	}

	public void setCheckPass ( boolean isCheckPass ) {
		this.isCheckPass = isCheckPass;
	}

	public boolean isCheckPassState ( ) {
		return checkPassState;
	}
	

	public void setCheckPassState ( boolean checkPassState ) {
		this.checkPassState = checkPassState;
	}

	public String getCheckedDefaultTargetUrl ( ) {
		return checkedDefaultTargetUrl;
	}

	public void setCheckedDefaultTargetUrl ( String checkedDefaultTargetUrl ) {
		this.checkedDefaultTargetUrl = checkedDefaultTargetUrl;
	}

}
